/*
 * @lc app=leetcode.cn id=557 lang=cpp
 *
 * [557] 反转字符串中的单词 III
 */

// @lc code=start
class Solution {
public:
    string reverseWords(string s) {
        int i = 0;
        while(i < s.size()){
            //i是单词开始，j是末尾+1,s[j] == ' '或者j==s.size()
            if (s[i] == ' ') i++;
            int j = i + 1;
            while(j < s.size() && s[j] != ' ') j++;
            reverse(s.begin() + i, s.begin() + j);
            i = j;
        }
        return s;
    }
};
// @lc code=end

